package net.m2technologies.open_arm.utilities.collections;

import org.apache.commons.collections.Predicate;

/**
 * Copyright 2005 Mark Masterson<br> <br> Licensed under the Apache License, Version 2.0 (the "License");<br> you may
 * not use this file except in compliance with the License.<br> You may obtain a copy of the License at<br> <br>
 * http://www.apache.org/licenses/LICENSE-2.0<br> <br> Unless required by applicable law or agreed to in writing,
 * software<br> distributed under the License is distributed on an "AS IS" BASIS,<br> WITHOUT WARRANTIES OR CONDITIONS
 * OF ANY KIND, either express or implied.<br> See the License for the specific language governing permissions and<br>
 * limitations under the License.<br>
 * <p/>
 * <p>Description: Abstract class to use the Predicate interface to handle checked exceptions. Normally, a selection
 * shouldn't cause any checked exceptions. Every checked exception is wrapped into a RuntimePredicateException. The user
 * of this class has to rewrite the #basicEvaluate method. The usage of this class may be combined with a special
 * #select method to clear the checked exception or unwrap it to throw it again.</p>
 *
 * @author Peter Ko�ek
 * @version 0.010
 */
public abstract class ExceptionalPredicate implements Predicate {

    /**
     * redefine the #evaluate method to establish a mechanism to tunnel checked exceptions through the
     * Commons-Collection API which doesn't know checked exceptions. The method is declared final because you should use
     * #basicEvaluate now.
     *
     * @see org.apache.commons.collections.Predicate#evaluate
     */
    public final boolean evaluate(final Object o) throws RuntimePredicateException {
        try {
            return this.basicEvaluate(o);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimePredicateException(e);
        }
    }

    /**
     * method to wrap any selecting code who can throw a checked exception. This method is called by the #evalutate
     * method of this class, which will wrap the exception into a RuntimePredicateException.
     */
    protected abstract boolean basicEvaluate(Object o) throws Exception;
}